import { defineStore } from "pinia";
import { ref } from "vue";
export const useCounterStore = defineStore(
  "cars",
  () => {
    let carts = ref([]);
    function addcart(data, id) {
      console.log(data, id);
      let username = localStorage.getItem("username");
      let usecart = carts.value.filter((item) => item.username == username);
      let pdcart = usecart.find((item) => item.goods.goods_id == id);
      if (pdcart) {
        pdcart.number++;
      } else {
        carts.value.push({
          username: username,
          if: Math.floor(Math.random() * 1000000),
          goods: data,
          checked: false,
          number: 1,
        });
      }
    }
    return { carts, addcart };
  },
  {
    persist: true,
  }
);
